package com.jcj.endecoder;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * 服务器端handler初始化配置类,在Channel注册到EventLoop后,对这个Channel添加一些初始化的Handler
 */
public class InitialzerServerEnDeCoder extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {

      /*  ChannelPipeline pipeline = socketChannel.pipeline();
        //pipeline.addLast("framer",new FixedLengthFrameDecoder(CommonTools.FIXEDLENGTHFRAME_LENGTH));
        //pipeline.addLast("framer",new LineBasedFrameDecoder(CommonTools.LINEBASEDFRAME_LENGTH,true,true));
        ByteBuf delimiter = Unpooled.copiedBuffer("$".getBytes());
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(CommonTools.DELIMITERBASEDFRAME_LENGTH, true, true, delimiter));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());
        pipeline.addLast(new HandlerServerDelimiterBased());  //自定义业务逻辑handler*/


        ChannelPipeline pipeline = socketChannel.pipeline();

        pipeline.addLast("framer",new LengthFieldBasedFrameDecoder(2048,3,4,0,0));

        pipeline.addLast(new HandlerServerLengthFieldBased());  //自定义业务逻辑handler

    }
}

















